package com.lz.action;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.lz.form.ItemCategoryDTO;
import com.lz.service.ItemCategoryService;
import com.lz.util.ConstantConfig;

public class ItemCategoryAction extends Action{

private String MULTI_CRUD_KEY="";
private String FORWARD="";
public void setMULTI_CRUD_KEY(String key){
MULTI_CRUD_KEY=key;
};

public ActionForward execute(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response)
 throws IOException, ServletException{

    if(!LoginAction.isAdmin(request)){
        request.setAttribute("message","You are not logged in as administrator");
	return mapping.findForward("MESSAGE");
    }

String CRUD=request.getParameter("crud");
FORWARD="ITEMCATEGORY"+CRUD.toUpperCase();

if(ConstantConfig.CRUD_INITCREATE.equalsIgnoreCase(CRUD)){
FORWARD="ITEMCATEGORY"+"CREATE";
}
else
if(ConstantConfig.CRUD_CREATE.equalsIgnoreCase(CRUD)){
create(form,request);
}
else
if(ConstantConfig.CRUD_UPDATE.equalsIgnoreCase(CRUD)){
update(form,request);
}
else
if(ConstantConfig.CRUD_DELETE.equalsIgnoreCase(CRUD)){
delete(form,request);
}
else
if(ConstantConfig.CRUD_QUERY.equalsIgnoreCase(CRUD)){
populate(form,request);
FORWARD="ITEMCATEGORY"+"UPDATE";
}
else
if(ConstantConfig.CRUD_QUERYALL.equalsIgnoreCase(CRUD)){
getAll(form,request);
}

return mapping.findForward(FORWARD);

}//eof execute


public  void create(ActionForm form,HttpServletRequest request){

ItemCategoryDTO dto=new ItemCategoryDTO();
dto.setCategoryid(request.getParameter("categoryid"));
dto.setCategoryname(request.getParameter("categoryname"));

ItemCategoryService.createBookcategory(dto);
getAll(form,request);
}



public  void update(ActionForm form,HttpServletRequest request){

ItemCategoryDTO dto=new ItemCategoryDTO();
dto.setCategoryid(request.getParameter("categoryid"));
dto.setCategoryname(request.getParameter("categoryname"));

ItemCategoryService.updateBookcategory(dto);getAll(form,request);
}



public  void delete(ActionForm form,HttpServletRequest request){

ItemCategoryDTO dto=new ItemCategoryDTO();
dto.setCategoryid(request.getParameter("categoryid"));

ItemCategoryService.deleteBookcategory(dto);getAll(form,request);
}



private  void populate(ActionForm form,HttpServletRequest request){

ItemCategoryDTO dto=new ItemCategoryDTO();
dto.setCategoryid(request.getParameter("categoryid"));

ItemCategoryService.populateBookcategory(dto);
request.setAttribute("dto",dto);
}



private  void getAll(ActionForm form,HttpServletRequest request){

List list=ItemCategoryService.getAllCategory();
request.setAttribute("list",list);
FORWARD="ITEMCATEGORY"+"SHOWALL";
}


}//eof class